package com.ssyan.appointment.cmn.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.ssyan.appointment.cmn.service.DictService;
import com.ssyan.appointment.common.result.Result;
import com.ssyan.appointment.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    //根据dictCode获取下级列表
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    //导入数据字典
    @ApiOperation("字典数据导入")
    @PostMapping("/import")
    public Result importDictData(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

    //导出数据字典
    @ApiOperation("将字典数据导出")
    @GetMapping("/export")
    public void exportDictData(HttpServletResponse response) throws IOException {
        dictService.exportDictData(response);
    }

    //根据数据id查询子数据列表
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChindData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        System.out.println(list);
        return Result.ok(list);

    }

    //根据dictcode和value值查询
    @GetMapping("getName/{dictCode}/{value}")
    public Result getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode,value);
        return Result.ok(dictName);
    }

    //根据value查询
    @GetMapping("getName/{value}")
    public Result getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);
        return Result.ok(dictName);
    }
}
